﻿@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_LayoutIndex.cshtml";
}@using LeSoft.Application.Code;
<script>
    var _organizeid = "@OperatorProvider.Provider.Current().CompanyId";
    var PStructId = "", CustomerId="";
    var PayWay = "";
    $(function () {
        InitialPage();
        GetGrid();
        GetTree();
    });
    //初始化页面
    function InitialPage() {
        //layout布局
        $('#layout').layout({
            applyDemoStyles: true,
            onresize: function () {
                $(window).resize()
            }
        });
        //resize重设(表格、树形)宽高
        $(window).resize(function (e) {
            window.setTimeout(function () {
                $('#gridTable').setGridWidth(($('.gridPanel').width()));
                $("#gridTable").setGridHeight($(window).height() - 165.5);
            }, 200);
            e.stopPropagation();
        });
        //收款方式
        $("#PayWay").ComboBoxTree({
            url: "../../SystemManage/DataItemDetail/GetDataItemTreeJson?Encode=PayWay",
            description: "==收款方式==",
            height: "150px",
            click: function (item) {
                PayWay = item.id;
                $('#btn_Search').trigger("click");
            }
        })
        $("#IsAudit").ComboBox({
            data: [{ "key": 0, "value": "未审核" }, { "key": 1, "value": "已审核" }],
            description: "全部",
            id: "key",
            text: "value",
            click: function (item) {
                $('#btn_Search').trigger("click");
            }
        });
        $("#OrganizeId").ComboBoxTree({
            url: "../../BaseManage/Organize/GetTreeJsonById",
            height: "250px",
            description: "===选择组织机构===",
            click: function (item) {
                _organizeid = item.id;
                GetTree();
                $('#btn_Search').trigger("click");
            }
        });
        $("#OrganizeId").ComboBoxSetValue(_organizeid);
         $("#StartTime").val("@DateTime.Now.AddDays(-DateTime.Now.Day+1).ToString("yyyy-MM-dd")");
         $("#EndTime").val("@DateTime.Now.AddMonths(1).AddDays(- DateTime.Now.Day).ToString("yyyy-MM-dd")");
        $("#time_horizon").ComboBox({
            data: [{ "key": 2, "value": "上周" }, { "key": 1, "value": "本周" }, { "key": 4, "value": "上月" }, { "key": 3, "value": "本月" }, { "key": 5, "value": "本年" }],
            id: "key",
            text: "value",
            //description: "==请选择==",
            click: function (item) {
                var value = item.attr('data-value');
                switch (value) {
                    case "1"://本周
                        $("#StartTime").val("@DateTime.Now.AddDays(1 - DateTime.Now.DayOfWeek.GetHashCode()).ToString("yyyy-MM-dd")");
                        $("#EndTime").val("@DateTime.Now.AddDays(7 - DateTime.Now.DayOfWeek.GetHashCode()).ToString("yyyy-MM-dd")");
                        break;
                    case "2"://上周
                        $("#StartTime").val("@DateTime.Now.AddDays(1 - DateTime.Now.DayOfWeek.GetHashCode()-7).ToString("yyyy-MM-dd")");
                        $("#EndTime").val("@DateTime.Now.AddDays(7 - DateTime.Now.DayOfWeek.GetHashCode()-7).ToString("yyyy-MM-dd")");
                        break;
                    case "3"://本月
                        $("#StartTime").val("@DateTime.Now.AddDays(-DateTime.Now.Day+1).ToString("yyyy-MM-dd")");
                        $("#EndTime").val("@DateTime.Now.AddMonths(1).AddDays(- DateTime.Now.Day).ToString("yyyy-MM-dd")");
                        break;
                    case "4"://上月
                        $("#StartTime").val("@DateTime.Now.AddDays(-DateTime.Now.Day + 1).AddMonths(-1).ToString("yyyy-MM-dd")");
                        $("#EndTime").val("@DateTime.Now.AddDays(-DateTime.Now.Day).ToString("yyyy-MM-dd")");
                        break;
                    case "5"://本年
                        $("#StartTime").val("@DateTime.Now.AddDays(-DateTime.Now.DayOfYear + 1).ToString("yyyy-MM-dd")");
                        $("#EndTime").val("@DateTime.Now.AddMonths(12 - DateTime.Now.Month + 1).AddDays(-DateTime.Now.Day).ToString("yyyy-MM-dd")");
                        break;
                    default:
                        break;
                }
                $("#SelectedStartTime").html($("#StartTime").val());
                $("#SelectedEndTime").html($("#EndTime").val());
                $('#btn_Search').trigger("click");
            }
        });
        $("#time_horizon").ComboBoxSetValue(3);
    }
    function GetTree() {
        var item = {
            height: $(window).height() - 50,
            url: "../../FinancialManage/ReceivableFeeItems/GetTree?OrganizeId=" + _organizeid,
            onnodeclick: function (item) {
                if (item.id == "All") {
                    PStructId = "";
                    CustomerId = "";
                }
                if (item.type == "PStruct") {
                    PStructId = item.id;
                    CustomerId = "";
                    if (item.id == "ALLPstructs") {
                        PStructId = "";
                    }
                }
                if (item.type == "Vendor") {
                    CustomerId = item.id;
                    PStructId = "";
                    if (item.id == "ALLelse") {
                        CustomerId = "";
                    }
                }
                $('#btn_Search').trigger("click");
            }
        };
        //初始化
        $("#itemTree1").treeview(item);
    }

    //加载表格
    function GetGrid() {
        var selectedRowIndex = 0;
        var $gridTable = $('#gridTable');
        var queryJson = {
            StartTime: $("#StartTime").val(),
            EndTime: $("#EndTime").val(),
            PStructId: PStructId,
            CustomerName: $("#CustomerName").val(),
            Code: $("#Code").val(),
            PayWay: PayWay,
            CustomerId: CustomerId,
            OrganzieId: _organizeid,
            IsAudit: $("#IsAudit").attr("data-value")
        };
        $gridTable.jqGrid({
            url: "../../FinancialManage/ReceiptBill/GetPageList",
            postData: { queryJson: JSON.stringify(queryJson) },
            datatype: "json",
            height: $(window).height() - 141,
            autowidth: true,
            sortable: true,
            colModel: [
                { label: "主键", name: "Id", hidden: true },
                { label: "单据编号", name: "Code", index: "Type", width: 150, align: "center", sortable: false },
                { label: "单据日期", name: "Date", index: "Date", width: 150, align: "center", sortable: false, formatter: "date", formatoptions: { newformat: 'Y-m-d' } },
                { label: "客户名称", name: "CustomerId", index: "CustomerId", width: 120, align: "center", sortable: false },
                { label: "房屋地址", name: "PStructId", index: "PStructId", width: 200, align: "center", sortable: false },
                { label: "收款方式", name: "PayWay", index: "PayWay", width: 100, align: "center", sortable: false },
                { label: "收款金额", name: "PayAmount", index: "PayAmount", width: 100, align: "center", sortable: false },
                { label: "收款人", name: "ReceivedUserId", index: "ReceivedUserId", width: 100, align: "center", sortable: false },
                { label: "票据类型", name: "InvoiceType", index: "InvoiceType", width: 100, align: "center", sortable: false },
                { label: "票据编号", name: "InvoiceNo", index: "InvoiceNo", width: 200, align: "center", sortable: false },
                {
                    label: "是否审核", name: "State", index: "State", width: 100, align: "center", sortable: false,
                    formatter: function (cellvalue) {
                        var Msg = "";
                        switch (cellvalue) {
                            case "1":
                            case 1: Msg = "<span style='Color:#009932;'>已审核</span>";
                                break;
                            case "0":
                            case 0: Msg = "<span style='Color:#F00;'>未审核</span>";
                                break;
                            default: Msg = "";
                                break;
                        }
                        return Msg;
                    }
                },
                { label: "审核人", name: "AuditUaerName", index: "AuditUaerName", width: 200, align: "center", sortable: false },
                { label: "审核时间", name: "AuditDate", index: "AuditDate", width: 200, align: "center", sortable: false },
                { label: "附加说明", name: "Description", index: "Description", width: 100, align: "center", sortable: false }
            ],
            viewrecords: true,
            rowNum: 30,
            rowList: [30, 50, 100],
            pager: "#gridPager",
            sortname: 'CreateDate',
            sortorder: 'desc',
            rownumbers: true,
            gridview: true,
            onSelectRow: function (status) {
                selectedRowIndex = $("#" + this.id).getGridParam('selrow');
                var State = $("#gridTable").jqGridRowValue("State");
                if (State.indexOf("未审核") >= 0) {
                    $("#lr-wrench").removeAttr("disabled");
                }
                else {
                    $("#lr-wrench").attr("disabled", "disabled");
                }
            },
            gridComplete: function () {
                $("#" + this.id).setSelection(selectedRowIndex, false);
            }
        });
        //查询条件
        $("#queryCondition .dropdown-menu li").click(function () {
            var text = $(this).find('a').html();
            var value = $(this).find('a').attr('data-value');
            $("#queryCondition .dropdown-text").html(text).attr('data-value', value)
        });
        //查询事件
        $("#btn_Search").click(function () {
            var queryJson = {
                StartTime: $("#StartTime").val(),
                EndTime: $("#EndTime").val(),
                PStructId: PStructId,
                CustomerName: $("#CustomerName").val(),
                Code: $("#Code").val(),
                PayWay: PayWay,
                CustomerId: CustomerId,
                OrganzieId: _organizeid,
                IsAudit: $("#IsAudit").attr("data-value")
            };
            $gridTable.jqGrid('setGridParam', {
                url: "../../FinancialManage/ReceiptBill/GetPageList",
                postData: { queryJson: JSON.stringify(queryJson) }
            }).trigger('reloadGrid');
        });


    }

    //编辑
    function btn_edit() {
        var keyValue = $("#gridTable").jqGridRowValue("Id");
        if (checkedRow(keyValue)) {
            dialogOpen({
                id: "Form",
                title: '收款单',
                url: '/FinancialManage/ReceiptBill/Form?keyValue=' + keyValue,
                width: "1100px",
                height: "800px",
                callBack: function (iframeId) {
                    top.frames["Form"].AcceptClick();
                },
            })
        }

    }
    //删除
    function btn_delete() {
        var keyValue = $("#gridTable").jqGridRowValue("Id");
        if (keyValue) {
            $.RemoveForm({
                url: '/FinancialManage/ReceiptBill/RemoveForm',
                param: { keyValue: keyValue },
                success: function (data) {
                    $("#gridTable").resetSelection();
                    $("#gridTable").trigger("reloadGrid");
                }
            })
        } else {
            dialogMsg('请选择需要删除的数据项！', 0);
        }
    }
</script>
<div class="ui-layout" id="layout" style="height: 100%; width: 100%;">
    <div class="ui-layout-west">
        <div class="west-Panel">
            <div class="panel-Title"><div id="OrganizeId" type="selectTree" class="ui-select "></div></div>
            <div id="itemTree1"></div>
        </div>
    </div>
    <div class="ui-layout-center">
        <div class="center-Panel">
            <div class="titlePanel">
                <div class="title-search">
                    <table>
                        <tr>
                            <td>筛选条件</td>
                            <td style="padding-left: 10px;">
                                <div class="ui-filter" style="width: 200px;">
                                    <div class="ui-filter-text">
                                        <strong id="SelectedStartTime"></strong> @DateTime.Now.AddDays(-DateTime.Now.Day + 1).ToString("yyyy-MM-dd")至 @DateTime.Now.AddMonths(1).AddDays(-DateTime.Now.Day).ToString("yyyy-MM-dd")<strong id="SelectedEndTime"></strong>
                                    </div>
                                    <div class="ui-filter-list" style="width: 350px;">
                                        <table class="form" id="filter-form">
                                            <tr>
                                                <th class="formTitle">单据日期：</th>
                                                <td class="formValue">
                                                    <input id="Category" type="hidden" value="1" />
                                                    <div style="float: left; width: 45%;">
                                                        <input id="StartTime" readonly type="text" value="@DateTime.Now.AddMonths(-1).ToString("yyyy-MM-dd")" class="form-control input-wdatepicker" onfocus="WdatePicker({maxDate:'%y-%M-%d'})">
                                                    </div>
                                                    <div style="float: left; width: 10%; text-align: center;">至</div>
                                                    <div style="float: left; width: 45%;">
                                                        <input id="EndTime" readonly type="text" value="@LeSoft.Util.Time.GetToday()" class="form-control input-wdatepicker" onfocus="WdatePicker({maxDate:'%y-%M-%d'})">
                                                    </div>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td class="formTitle">单据编号：</td>
                                                <td class="formValue">
                                                    <input id="Code" type="text" class="form-control">
                                                </td>
                                            </tr>
                                            <tr>
                                                <td class="formTitle">客户名称：</td>
                                                <td class="formValue">
                                                    <input id="CustomerName" type="text" class="form-control">
                                                </td>
                                            </tr>
                                        </table>
                                        <div class="ui-filter-list-bottom">
                                            <a id="btn_Reset" class="btn btn-default">&nbsp;重&nbsp;&nbsp;置</a>
                                            <a id="btn_Search" class="btn btn-primary">&nbsp;查&nbsp;&nbsp;询</a>
                                        </div>
                                    </div>
                                </div>
                            </td>
                            <td style="padding-left: 10px;">
                                <div id="time_horizon" type="selectTree" class="ui-select" style="width: 60px;"></div>
                            </td>
                            <td><div id="PayWay" type="selectTree" style="width: 120px; margin-left:5px;" class="ui-select "></div></td>
                            <td><div id="IsAudit" type="selectTree" style="width: 80px; margin-left:5px;" class="ui-select "></div></td>
                         </tr>
                    </table>
                </div>
                <div class="toolbar">
                    <div class="btn-group">
                        <a id="lr-replace" class="btn btn-default" onclick="reload(); "><i class="fa fa-refresh"></i>&nbsp;刷新</a>
                        <a id="lr-edit" class="btn btn-default" onclick="btn_edit()"><i class="fa fa-pencil-square-o"></i>&nbsp;查看</a>
                        <a id="lr-delete" class="btn btn-default" onclick="btn_delete()"><i class="fa fa-trash-o"></i>&nbsp;删除</a>
                        <a id="lr-wrench" class="btn btn-default" disabled onclick="btn_audit()"><i class="fa fa-wrench"></i>&nbsp;审核</a>
                        <div class="btn-group">
                            <a id="lr-more" class="btn btn-default" data-toggle="dropdown"><i class="fa fa-reorder"></i>&nbsp;更多操作<span class="caret"></span></a>
                        </div>
                    </div>

                </div>
            </div>
            <div class="gridPanel">
                <table id="gridTable"></table>
                <div id="gridPager"></div>
            </div>
        </div>
    </div>
</div>

